# 基金历史净值数据

# Request URL: http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18305454313767197976_1649780309713&fundCode=000001&pageIndex=1&pageSize=20&startDate=&endDate=&_=1649780313710
import sqlite3
from turtle import pd

import pandas
import requests
import json
import re
import pandas as pd


def get_data(name,code,page=1):
    # 伪装请求头
    headers = {
        # ctrl+r    (.*): (.*)替换'$1':'$2',
        'Cookie': 'qgqp_b_id=e1d7a84f522c1ca42156044d65056541; st_si=74663039780832; st_asi=delete; EMFUND1=null; EMFUND2=null; EMFUND3=null; EMFUND4=null; EMFUND5=null; EMFUND6=null; EMFUND7=null; EMFUND0=null; EMFUND8=04-12%2022%3A54%3A06@%23%24%u5E7F%u53D1%u9053%u743C%u65AF%u77F3%u6CB9%u6307%u6570%u4EBA%u6C11%u5E01C@%23%24004243; EMFUND9=04-12 22:57:16@#$%u6CF0%u4FE1%u884C%u4E1A%u7CBE%u9009%u6DF7%u5408A@%23%24290012; st_pvi=89163971024888; st_sp=2022-04-12%2016%3A24%3A17; st_inirUrl=http%3A%2F%2Ffundf10.eastmoney.com%2F; st_sn=51; st_psi=20220413001829628-112200305283-7023313978',
        # 防盗链
        'Referer': 'http://fundf10.eastmoney.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36',
    }

    df_list = []        #空列表
    # 每个基金历史净值页
    for index in range(1,3):
        url = 'http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18305454313767197976_1649780309713&fundCode={}&pageIndex={}&pageSize=20&startDate=&endDate=&_=1649780313710'.format(code,index)
        # 发送请求 接收数据
        response = requests.get(url,headers = headers)
        ''' print(response.text) '''
        html = response.text
        ''' print(html) '''
        # 正则表达式
        res = re.findall('\((.*?)\)',html)
        datas = json.loads(res[0])["Data"]['LSJZList']  # 转化字典
        ''' print(datas) '''

        df = pd.DataFrame(datas)        # 二维表
        df_list.append(df)          # 追加相连

    df_data = pd.concat(df_list)

    df_data.to_csv('./csv/{}.csv'.format(name),index=False)       #保存数据在这里！！！！！！！
    conn = sqlite3.connect("fundDB.db")
    df = pandas.read_csv('./csv/{}.csv'.format(name))
    df.to_sql('{}'.format(name), conn, if_exists='append', index=False)
    conn.close()


def get_fund_ranking(num):
    # 伪装请求头
    headers = {
        # ctrl+r    (.*): (.*)替换'$1':'$2',
        'Cookie':'qgqp_b_id=e1d7a84f522c1ca42156044d65056541; st_si=74663039780832; st_asi=delete; ASP.NET_SessionId=0gvzzbmh0w3g5kegsp25d3rw; _adsame_fullscreen_16928=1; st_pvi=89163971024888; st_sp=2022-04-12%2016%3A24%3A17; st_inirUrl=http%3A%2F%2Ffundf10.eastmoney.com%2F; st_sn=28; st_psi=20220412203526936-112200312936-1289546186',
        'Host':'fund.eastmoney.com',
        # 防盗链
        'Referer':'http://fund.eastmoney.com/data/fundranking.html',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36',
    }
    base_url = 'http://fund.eastmoney.com/data/rankhandler.aspx?op=ph&dt=kf&ft=all&rs=&gs=0&sc=6yzf&st=desc&sd=2021-04-13&ed=2022-04-13&qdii=&tabSubtype=,,,,,&pi={}&pn=50&dx=1&v=0.5636734146918101'.format(num)
    response = requests.get(base_url,headers=headers)
    '''print(response.text)'''

    result = re.findall('"(.*?)"',response.text)
    print(result)
    '''print(result[0])'''

    for i in result:
        code = i.split(',')[0]
        name = i.split(',')[1]
        get_data(name,code)     #   name,code顺序不可以反过来




## 可视化
# import matplotlib.pyplot as plt
# import mplcyberpunk
#
# df1 = pd.read_sql('万家精选混合',engine='python')
#
#
# plt.style.use("cyberpunk")
# plt.plot(df1['DWJZ'].to_list()[::-1],marker='o') # 切片逆序
# # 题目中文化
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
# plt.title('万家精选混合')
# plt.ylabel('DWJZ')
#
# mplcyberpunk.add_glow_effects()
# plt.show()



